package com.reggie.controller;

import com.reggie.pojo.AddressBook;
import com.reggie.pojo.User;
import com.reggie.service.AddressBookService;
import com.reggie.util.ResultUtil;
import com.reggie.util.SnowflakeIdWorkerUtil;
import com.reggie.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//地址请求映射类
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    //查询该用户的所有地址信息
    @GetMapping("/list")
    public ResultUtil<List<AddressBook>> list(){
        Map<String, Object> map = ThreadLocalUtil.get();
        List<AddressBook> addressBooks = addressBookService.findAll(Long.valueOf((String) map.get("id")));
        if (addressBooks==null) return ResultUtil.error("获取地址信息失败！");
        return ResultUtil.success(addressBooks);
    }

    //添加新地址
    @PostMapping
    public ResultUtil<String> add(@RequestBody AddressBook addressBook){
        Long id = new SnowflakeIdWorkerUtil(0,0).nextId();
        addressBook.setId(id);
        Map<String, Object> map = ThreadLocalUtil.get();
        addressBook.setUserId(Long.valueOf((String) map.get("id")));
        addressBook.setCreateUser(Long.valueOf((String) map.get("id")));
        addressBook.setUpdateUser(Long.valueOf((String) map.get("id")));
        if (addressBookService.add(addressBook)) return ResultUtil.success("添加地址信息成功！");
        return ResultUtil.error("添加地址信息失败！");
    }

    //更新地址信息
    @PutMapping
    public ResultUtil<String> update(@RequestBody AddressBook addressBook){
        if (addressBookService.edit(addressBook)) return ResultUtil.success("更新地址信息成功！");
        return ResultUtil.error("更新地址信息失败！");
    }

    //删除地址信息
    @DeleteMapping
    public ResultUtil<String> delete(String ids){
        Boolean isSuccess = true;
        Map<String, Object> map = ThreadLocalUtil.get();
        for (String id:ids.split(",")) {
            AddressBook addressBook = new AddressBook();
            addressBook.setUserId(Long.valueOf((String) map.get("id")));
            addressBook.setId(Long.valueOf(id));
            addressBook.setUpdateUser(Long.valueOf((String) map.get("id")));
            addressBook.setIsDefault(0);
            addressBook.setIsDeleted(1);
            if (addressBookService.edit(addressBook)) continue;
            isSuccess = false;
        }
        if (isSuccess) return ResultUtil.success("删除地址信息成功！");
        return ResultUtil.error("删除地址信息失败");
    }

    //根据id查询单个地址
    @GetMapping("/{id}")
    public ResultUtil<AddressBook> list(@PathVariable("id") Long id){
        AddressBook addressBook = addressBookService.find(id, null, null);
        if (addressBook==null) return ResultUtil.error("获取地址信息失败！");
        return ResultUtil.success(addressBook);
    }

    //设置默认地址
    @PutMapping("/default")
    public ResultUtil<String> setDefault(@RequestBody AddressBook addressBook){
        Map<String, Object> map = ThreadLocalUtil.get();
        AddressBook oldDefault = addressBookService.find(null, Long.valueOf((String) map.get("id")), 1);
        if (oldDefault!=null){
            oldDefault.setIsDefault(0);
            oldDefault.setUpdateUser(Long.valueOf((String) map.get("id")));
            addressBookService.edit(oldDefault);
        }
        AddressBook newDefault = addressBookService.find(addressBook.getId(), null, null);
        newDefault.setIsDefault(1);
        if (newDefault!=null) if (addressBookService.edit(newDefault)) return ResultUtil.success("设置默认地址成功！");
        return ResultUtil.error("设置默认地址失败！");
    }

    //获取默认地址
    @GetMapping("/default")
    public ResultUtil<AddressBook> getDefault(){
        Map<String, Object> map = ThreadLocalUtil.get();
        AddressBook addressBook = addressBookService.find(null, Long.valueOf((String) map.get("id")), 1);
        if (addressBook==null) return ResultUtil.error("获取默认地址失败！");
        return ResultUtil.success(addressBook);
    }
}
